/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xylogyauth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xylogyauth.entity.XyLogyAuth;
import vip.xiaonuo.modular.xylogyauth.enums.XyLogyAuthExceptionEnum;
import vip.xiaonuo.modular.xylogyauth.mapper.XyLogyAuthMapper;
import vip.xiaonuo.modular.xylogyauth.param.XyLogyAuthParam;
import vip.xiaonuo.modular.xylogyauth.service.XyLogyAuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 用户与族谱得权限表service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:27
 */
@Service
public class XyLogyAuthServiceImpl extends ServiceImpl<XyLogyAuthMapper, XyLogyAuth> implements XyLogyAuthService {

    @Override
    public PageResult<XyLogyAuth> page(XyLogyAuthParam xyLogyAuthParam) {
        QueryWrapper<XyLogyAuth> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyLogyAuthParam)) {

            // 根据用户编号 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getUserId())) {
                queryWrapper.lambda().eq(XyLogyAuth::getUserId, xyLogyAuthParam.getUserId());
            }
            // 根据族谱编号 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getLogyId())) {
                queryWrapper.lambda().eq(XyLogyAuth::getLogyId, xyLogyAuthParam.getLogyId());
            }
            // 根据*,代表全部,指定家族成员;多选，层级编号，也就是 ethnic id,所有派系下得 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getPerMisc())) {
                queryWrapper.lambda().eq(XyLogyAuth::getPerMisc, xyLogyAuthParam.getPerMisc());
            }
            // 根据权限名称 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getAuthName())) {
                queryWrapper.lambda().eq(XyLogyAuth::getAuthName, xyLogyAuthParam.getAuthName());
            }
            // 根据备注信息 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getReMark())) {
                queryWrapper.lambda().eq(XyLogyAuth::getReMark, xyLogyAuthParam.getReMark());
            }
            // 根据是否禁用,1=正常,2=禁用 查询
            if (ObjectUtil.isNotEmpty(xyLogyAuthParam.getDisabled())) {
                queryWrapper.lambda().eq(XyLogyAuth::getDisabled, xyLogyAuthParam.getDisabled());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyLogyAuth> list(XyLogyAuthParam xyLogyAuthParam) {
        return this.list();
    }

    @Override
    public void add(XyLogyAuthParam xyLogyAuthParam) {
        XyLogyAuth xyLogyAuth = new XyLogyAuth();
        BeanUtil.copyProperties(xyLogyAuthParam, xyLogyAuth);
        this.save(xyLogyAuth);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyLogyAuthParam> xyLogyAuthParamList) {
        xyLogyAuthParamList.forEach(xyLogyAuthParam -> {
            this.removeById(xyLogyAuthParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyLogyAuthParam xyLogyAuthParam) {
        XyLogyAuth xyLogyAuth = this.queryXyLogyAuth(xyLogyAuthParam);
        BeanUtil.copyProperties(xyLogyAuthParam, xyLogyAuth);
        this.updateById(xyLogyAuth);
    }

    @Override
    public XyLogyAuth detail(XyLogyAuthParam xyLogyAuthParam) {
        return this.queryXyLogyAuth(xyLogyAuthParam);
    }

    /**
     * 获取用户与族谱得权限表
     *
     * @author yanwei
     * @date 2023-05-05 15:44:27
     */
    private XyLogyAuth queryXyLogyAuth(XyLogyAuthParam xyLogyAuthParam) {
        XyLogyAuth xyLogyAuth = this.getById(xyLogyAuthParam.getId());
        if (ObjectUtil.isNull(xyLogyAuth)) {
            throw new ServiceException(XyLogyAuthExceptionEnum.NOT_EXIST);
        }
        return xyLogyAuth;
    }

    @Override
    public void export(XyLogyAuthParam xyLogyAuthParam) {
        List<XyLogyAuth> list = this.list(xyLogyAuthParam);
        PoiUtil.exportExcelWithStream("SnowyXyLogyAuth.xls", XyLogyAuth.class, list);
    }

}
